//
//  PPComplainViewController.m
//  pickpic
//
//  Created by GaoYongyue on 16/10/7.
//  Copyright © 2016年 MicroDream. All rights reserved.
//

#import "PPComplainViewController.h"

@interface PPComplainViewController ()<UITextViewDelegate>
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (weak, nonatomic) IBOutlet UIView *line1;
@property (weak, nonatomic) IBOutlet UIView *line2;
@property (weak, nonatomic) IBOutlet UIView *line3;
@property (weak, nonatomic) IBOutlet UIView *line4;
@property (weak, nonatomic) IBOutlet UIView *line5;
@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@property (weak, nonatomic) IBOutlet UIImageView *imageView3;
@property (weak, nonatomic) IBOutlet UIImageView *imageView4;
@property (weak, nonatomic) IBOutlet UILabel *otherReasonLabel;
@property (weak, nonatomic) IBOutlet UITextView *otherReasonTextView;
@property (weak, nonatomic) IBOutlet UILabel *otherReasonWordsCountLabel;

@property (nonatomic, assign) NSInteger selectedRow;
@end

@implementation PPComplainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"投诉";
    [_line1 addTopBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line2 addTopBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line3 addTopBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line4 addTopBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line5 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];

    _selectedRow = -1;
    _otherReasonTextView.hidden = YES;
    _otherReasonWordsCountLabel.hidden = YES;
    _otherReasonTextView.delegate = self;

    __weak typeof(self)weakSelf = self;
    [self setNavigationRightTitle:@"发送" titleColor:[UIColor colorWithRGBHex:0xffdd17] action:^{
        [weakSelf saveItemDidPressed];
    }];

    self.navigationItem.rightBarButtonItem.enabled = NO;
}

- (void)saveItemDidPressed
{
    __weak typeof(self)weakSelf = self;

    [_otherReasonTextView resignFirstResponder];
    NSString *content = nil;
    if (_selectedRow == 0)
    {
        content = @"色情低俗";
    }
    else if (_selectedRow == 1)
    {
        content = @"政治敏感";
    }
    else if (_selectedRow == 2)
    {
        content = @"广告骚扰";
    }
    else if (_selectedRow == 3)
    {
        content = _otherReasonTextView.text;

        if ([content length] == 0 || [content isEmptyOrWhitespace])
        {
            [self.navigationController.view toastMessage:@"投诉内容不能为空哦" duration:1.f];
            return;
        }
        else if ([content length] > 140)
        {
            [self.navigationController.view toastMessage:@"内容不能超过140字哦" duration:1.f];
            return;
        }
    }
    if ([content length] == 0)
    {
        [self.navigationController.view toastMessage:@"投诉内容不能为空哦" duration:1.f];
        return;
    }
    //投诉类型1:图册;2:图片;4:用户;8:评论;  对应相册id,或图片id,或用户id,或评论id.
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    [PPNetworkService createComplaintWithType:_type releationID:_relationID content:content category:@(_selectedRow + 1) callback:^(id data, NSError *error) {
        [hud hide:YES];
        if (data) {
            [weakSelf.navigationController.view toastMessage:@"发送成功" duration:1.f completion:^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }];
        }
        else
        {
            [weakSelf.navigationController.view toastMessage:error.networkPrompt duration:1.f];
        }
    }];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 3 && _selectedRow == 3) {
        return 150;
    }
    return 44;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        _imageView1.hidden = _selectedRow == 0 ? NO : YES;
    }
    else if (indexPath.row == 1)
    {
        _imageView2.hidden = _selectedRow == 1 ? NO : YES;
    }
    else if (indexPath.row == 2)
    {
        _imageView3.hidden = _selectedRow == 2 ? NO : YES;
    }
    else if (indexPath.row == 3)
    {
        _imageView4.hidden = _selectedRow == 3 ? NO : YES;

        _otherReasonLabel.hidden = _selectedRow == 3 ? YES : NO;
        _otherReasonTextView.hidden = _selectedRow == 3 ? NO : YES;
        _otherReasonWordsCountLabel.hidden = _otherReasonTextView.hidden;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    self.navigationItem.rightBarButtonItem.enabled = YES;
    self.selectedRow = indexPath.row;

    [self.tableView reloadData];
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView
{
    _otherReasonWordsCountLabel.text = [NSString stringWithFormat:@"%ld/140", [textView.text length]];
    _otherReasonWordsCountLabel.textColor = [textView.text length] > 140 ? [UIColor redColor] : [UIColor colorWithRGBHex:0xb2b2b2];
}




@end
